package com.tjbank.cssys.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tjbank.cssys.api.CkOutsourcingCompanyService;
import com.tjbank.cssys.api.OrganService;
import com.tjbank.cssys.dto.CkOutsourcingCompanyDTO;
import com.tjbank.cssys.dto.OrganDTO;
import com.tjbank.cssys.mybatis.entity.CkOutsourcingCompany;
import com.tjbank.cssys.mybatis.entity.CkOutsourcingCompanyAcceptLevel;
import com.tjbank.cssys.mybatis.entity.CkOutsourcingCompanyRate;
import com.tjbank.cssys.mybatis.mapper.CkOutsourcingCompanyAcceptLevelMapper;
import com.tjbank.cssys.mybatis.mapper.CkOutsourcingCompanyMapper;
import com.tjbank.cssys.mybatis.mapper.CkOutsourcingCompanyRateMapper;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CkOutsourcingCompanyServiceImpl implements CkOutsourcingCompanyService {
    @Autowired
    private CkOutsourcingCompanyMapper ckOutsourcingCompanyMapper;
    @Autowired
    private OrganService organService;
    protected static final Logger logger = LoggerFactory.getLogger(CkOutsourcingCompanyServiceImpl.class);
    @Autowired
    private CkOutsourcingCompanyAcceptLevelMapper levelMapper;
    @Autowired
    private CkOutsourcingCompanyRateMapper rateMapper;

    /**
     * 添加委外公司
     * @param ckOutsourcingCompanyDTO
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void addOutsourcingCompany(CkOutsourcingCompanyDTO ckOutsourcingCompanyDTO, String levels, JSONArray rateList) {
        logger.info("添加委外公司和机构start---参数：ckOutsourcingCompany:"+ JSON.toJSONString(ckOutsourcingCompanyDTO));
        OrganDTO organDTO = new OrganDTO();
        BeanUtils.copyProperties(ckOutsourcingCompanyDTO,organDTO);
        organDTO = JSON.parseObject(JSON.toJSONString(organService.addOrgan(organDTO)), OrganDTO.class);
        String id = organDTO.getId();
        Date date = new Date();
        ckOutsourcingCompanyDTO.setUpdateTime(date);
        CkOutsourcingCompany ckOutsourcingCompany = new CkOutsourcingCompany();
        BeanUtils.copyProperties(ckOutsourcingCompanyDTO,ckOutsourcingCompany);
        ckOutsourcingCompany.setMechanismId(id);
        ckOutsourcingCompany.setOutCompanyCode(organDTO.getCode());
        if(StringUtils.isNotBlank(ckOutsourcingCompanyDTO.getState()) && !"1".equals(ckOutsourcingCompanyDTO.getState())){
            ckOutsourcingCompany.setState(2);
        }else {
            ckOutsourcingCompany.setState(1);
        }
        ckOutsourcingCompanyMapper.insert(ckOutsourcingCompany);
        System.out.println("ckOutsourcingCompany==="+JSON.toJSONString(ckOutsourcingCompany));

        /**分案的逾期期数**/
        if(null!=levels && !levels.equals("")){
            String[] split = levels.split(",");
            List<CkOutsourcingCompanyAcceptLevel> acceptLevels = new ArrayList<>();
            for (String levelName:split) {
                CkOutsourcingCompanyAcceptLevel acceptLevel = new CkOutsourcingCompanyAcceptLevel();
                acceptLevel.setCompanyId(ckOutsourcingCompany.getId().intValue());
                acceptLevel.setLevelName(levelName);
                acceptLevel.setIsDelete("1");
                acceptLevels.add(acceptLevel);
            }
            if(acceptLevels.size()>0){
                levelMapper.batchInsert(acceptLevels);
            }
        }
        /**结算的分等级比率**/
        if(rateList.size()>0){
            List<CkOutsourcingCompanyRate> rates = new ArrayList<CkOutsourcingCompanyRate>();
            List<CkOutsourcingCompanyRate> maps = JSONObject.parseArray(rateList.toJSONString(), CkOutsourcingCompanyRate.class);
            for (CkOutsourcingCompanyRate mm : maps) {
                mm.setCompanyId(ckOutsourcingCompany.getId().intValue());
                mm.setCompanyName(ckOutsourcingCompany.getOutsourcingCompany());
                mm.setCheckModel(ckOutsourcingCompany.getCheckModel());

                mm.setIsDelete("1");

                rates.add(mm);
            }
            if(rates.size()>0){
                rateMapper.batchInsert(rates);
            }
        }
        logger.info("添加委外公司和机构end");
    }

}
